<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="关键字" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.state" clearable placeholder="请选择">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="dataForm.date" type="datetimerange" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('hotel:thotelfloorroomcheckinpeopleinvoice:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('hotel:thotelfloorroomcheckinpeopleinvoice:delete')" type="danger" @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>


      <el-form-item style="float: right;" label="" v-if="isAuth('hotel:hotelseller:list')">
        <selectSeller @change="selectSellerChange"></selectSeller>
      </el-form-item>

    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="150">
      </el-table-column>

      <el-table-column prop="type" header-align="center" align="center" sortable label="发票类型">
      </el-table-column>
      <el-table-column prop="sysNo" header-align="center" align="center" sortable label="登记单号">
      </el-table-column>
      <el-table-column prop="roomSignNum" header-align="center" align="center" sortable label="房间号">
      </el-table-column>
      <el-table-column prop="panme" header-align="center" align="center" sortable label="客人姓名">
      </el-table-column>
      <el-table-column prop="arrivalsTime" header-align="center" align="center" sortable label="入住时间">
      </el-table-column>
      <el-table-column prop="predissociationTime" header-align="center" align="center" sortable label="预离时间">
      </el-table-column>
      <el-table-column prop="checkOutTime" header-align="center" align="center" sortable label="退房时间">
      </el-table-column>
      <el-table-column prop="consumption" header-align="center" align="center" sortable label="总消费">
      </el-table-column>
      <el-table-column prop="number" header-align="center" align="center" sortable label="发票号码">
      </el-table-column>
      <el-table-column prop="price" header-align="center" align="center" sortable label="发票金额">
      </el-table-column>
      <el-table-column prop="note" header-align="center" align="center" sortable label="备注">
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" sortable label="创建时间">
      </el-table-column>
      <el-table-column prop="statetext" header-align="center" align="center" sortable label="状态">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="disCardHandle(scope.row.id)">{{maps[scope.row.state]}}</el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './thotelfloorroomcheckinpeopleinvoice-add-or-update'
  export default {
    data() {
      return {
        options: [{
          value: '0',
          label: '废弃'
        }, {
          value: '1',
          label: '正常'
        }],
        maps: {
          1: '废弃',
          0: '恢复'
        },
        dataForm: {
          key: '',
          state: '',
          date: [],
          startDate: '',
          endDate: ''
        },
        dataList: [],
        sellerIds: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated() {
      this.getDataList()
    },
    methods: {
      selectSellerChange(val) {
        console.log("接收值", val);
        this.sellerIds = val;
        this.getDataList();
      },
      gettime(time) {
        var year = time.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = time.getMonth() + 1; //获取当前月份(0-11,0代表1月)
        var day = time.getDate(); //获取当前日(1-31)
        if (month < 10) {
          month = "0" + month;
        }
        if (day < 10) {
          day = "0" + day;
        }
        var h = time.getHours(); // 获取当前小时数(0-23)
        var s = time.getMinutes(); // 获取当前分钟数(0-59)
        var a = time.getSeconds();
        if (h < 10) {
          h = "0" + h;
        }
        if (s < 10) {
          s = "0" + s;
        }
        if (a < 10) {
          a = "0" + a;
        }

        var dateString = year + "-" + month + "-" + day + "  " + h + ":" + s + ":" + a;
        return dateString;
      },
      // 获取数据列表
      getDataList() {
        if (this.dataForm.date != null) {
          this.dataForm.startDate = this.dataForm.date[0];
          this.dataForm.endDate = this.dataForm.date[1];
          if (this.dataForm.startDate != null && this.dataForm.startDate != "") {
            if (this.dataForm.startDate instanceof Date) {
              this.dataForm.startDate = this.gettime(this.dataForm.startDate);
            }

          }
          if (this.dataForm.endDate != null && this.dataForm.endDate != "") {
            if (this.dataForm.endDate instanceof Date) {
              this.dataForm.endDate = this.gettime(this.dataForm.endDate);
            }

          }
        } else {
          this.dataForm.startDate = "";
          this.dataForm.endDate = "";
        }


        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/hotel/thotelfloorroomcheckinpeopleinvoice/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            'sellerIds': this.sellerIds.join(","),
            'startDate': this.dataForm.startDate,
            'endDate': this.dataForm.endDate,
            'state': this.dataForm.state
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.size
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      disCardHandle(id) {
        this.$http({
          url: this.$http.adornUrl('/hotel/thotelfloorroomcheckinpeopleinvoice/discard'),
          method: 'post',
          data: this.$http.adornData(id, false)
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })

      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/hotel/thotelfloorroomcheckinpeopleinvoice/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
